﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CryptoUtils
{
    public partial class EncryptDecrypt : Form
    {
        public EncryptDecrypt()
        {
            InitializeComponent();
        }

        private void EncryptDecrypt_Load(object sender, EventArgs e)
        {
            this.labelError.Visible = false;
        }

        private void buttonEncode_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textBoxKeyPhrase.Text.Trim()))
            {
                this.labelError.Visible = false;
                this.textBoxCipherText.Text = CryptoUtils.Encrypt((this.textBoxPlainText.Text.Trim()), this.textBoxKeyPhrase.Text.Trim(), this.checkBoxIsConnectionString.Checked);
            }
            else
            {
                this.labelError.Visible = true;
            }
        }

        private void buttonDecode_Click(object sender, EventArgs e)
        {
            if (this.checkBoxIsConnectionString.Checked)
                this.textBoxPlainText.Text = CryptoUtils.DecryptWithEmbeddedKeyPhrase(this.textBoxCipherText.Text.Trim());
            else
            {
                if (!string.IsNullOrEmpty(this.textBoxKeyPhrase.Text.Trim()))
                {
                    this.labelError.Visible = true;
                    return;
                }
                this.textBoxPlainText.Text = CryptoUtils.Decrypt(this.textBoxCipherText.Text.Trim(), this.textBoxKeyPhrase.Text.Trim());
            }
        }

        private void buttonComputeHash_Click(object sender, EventArgs e)
        {
            this.textBoxCipherText.Text = CryptoUtils.ComputeHash(this.textBoxPlainText.Text.Trim());
        }

        private void buttonURLEncode_Click(object sender, EventArgs e)
        {
            this.textBoxCipherText.Text = System.Web.HttpUtility.UrlEncode(this.textBoxPlainText.Text.Trim());
        }

        private void buttonURLDecode_Click(object sender, EventArgs e)
        {
            this.textBoxPlainText.Text = System.Web.HttpUtility.UrlDecode(this.textBoxCipherText.Text.Trim());
        }
    }
}
